package cn.edu.jou.cs.course.springboot.addersim.fulladder;

import cn.edu.jou.cs.course.springboot.addersim.halfadder.Adder;
import cn.edu.jou.cs.course.springboot.addersim.Signal;

public class FullAdder {
	Adder halfAdder1 = new Adder();
	Adder halfAdder2 = new Adder();

	Output add(Input input) {
		cn.edu.jou.cs.course.springboot.addersim.halfadder.Output output1 = halfAdder1.add(
			new cn.edu.jou.cs.course.springboot.addersim.halfadder.Input(input.getAugend(), input.getAddend()));
		
		cn.edu.jou.cs.course.springboot.addersim.halfadder.Input input2 = new cn.edu.jou.cs.course.springboot.addersim.halfadder.Input(
			output1.getSum(),
			input.getCarry());

		cn.edu.jou.cs.course.springboot.addersim.halfadder.Output output2 = halfAdder2.add(input2);

		Signal carry = orSignal(output1.getCarry(), output2.getCarry());

		return new Output(output2.getSum(), carry);
	}

	Signal orSignal(Signal a, Signal b) {
		return Signal.valueOf(a.getValue() |  b.getValue());
	}
}